/*
Copyright (c) 2006 - 2008  Universitaet Osnabrueck, virtUOS
Authors: Markus Ketterl, Johannes Emden, Nils Schuhmacher, Andreas Rickling

This file is part of virtPresenter.

virtPresenter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

virtPresenter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with virtPresenter.  If not, see <http://www.gnu.org/licenses/>.
*/

package virtPresenter.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.controls.Alert;
	
	import virtPresenter.event.*;
	import virtPresenter.model.TimeStep;
	import virtPresenter.model.virtPresenterLocator;
	
	public class VideoPlayerChange implements ICommand
	{
		public function execute(event:CairngormEvent):void
		{
			try{
				
				var model : virtPresenterLocator = virtPresenterLocator.getInstance();			
				
				if(model.xmlReceived)
				{
					var VideoPlayerTimeChange : VideoPlayerTimeChangeEvent =VideoPlayerTimeChangeEvent( event );
					model.currentvideotime=VideoPlayerTimeChange.currentTime;
					
					if(isNaN(model.currentvideotime))
						model.currentvideotime=0;
					
					//trace("VideoPlayerChange. Given videotime:" +VideoPlayerTimeChange.currentTime);
					
					var videotime_msec:int=model.currentvideotime*1000;					
					var timy:TimeStep=model.xmlParser.getTimeStepByTime(videotime_msec);
					var slideID:int=timy.getID();
					var timestamp_currentStep:int=timy.getStep();
					var animStep:int=timy.getAnimationStep();
					var slides:Array=model.xmlParser.slides;
					
					var prev:TimeStep=model.xmlParser.getPreviousStep(videotime_msec);
					var prevSlideID:int=prev.getID();
					var prevAnimStep:int=prev.getAnimationStep();
					
					var nextS:TimeStep=model.xmlParser.getNextStep(videotime_msec);
					var nextSlideID:int=nextS.getID();
					var nextAnimStep:int=nextS.getAnimationStep();
					
					
					
					
					//model.isFirst = model.xmlParser.isErster(timy);
					//model.isLast = model.xmlParser.isLetzter(timy);
					
					
					
					//Set the selected index in the slideBrowser
					if(model.selectedThumbnailIndex==model.xmlParser.getSlideIndexByTime(videotime_msec))
					{}
					else
					{
						trace("VideoPlayerChange. ThumbnailIndex set to: "+model.xmlParser.getSlideIndexByTime(videotime_msec));
						model.selectedThumbnailIndex=model.xmlParser.getSlideIndexByTime(videotime_msec);
					}
					
					//If end of the video is reached, go back to beginning.
					if(model.currentvideotime>model.metaData.duration-1){
						CairngormEventDispatcher.getInstance().dispatchEvent(new PlayStopVideoPlayerEvent(PlayStopVideoPlayerEvent.STOP));
					}				
					
					
					
					//trace("VideoPlayerChange. Current videotime :"+model.currentvideotime+". Videoduration: "+model.metaData.duration);
					
					//If the current videotime fits with the currently shown slide look, if
					//the animationstep fits, otherwise change to the corresponding animationstep or slide.				
					
					
					
					if (model.currentSlideID==slideID)
					{
						if(model.currentAnimStep==animStep)
						{
							//trace("VideoPlayerChange. Everything okay, do nothing!");
							
						}
						else
						{
							trace("VideoPlayerChange. Go to animation Step: "+animStep);
							if((model.prevSID!=prevSlideID)||(model.nextSID!=nextSlideID)){
								CairngormEventDispatcher.getInstance().dispatchEvent(new GoToSlideEvent(slideID,prevSlideID,nextSlideID));
							}
							CairngormEventDispatcher.getInstance().dispatchEvent(new GoToAnimationStepEvent(animStep,prevAnimStep,nextAnimStep));
							
						}	
						
					}
						
					else
					{
						trace("VideoPlayerChange. Go to slide: "+slideID);
						CairngormEventDispatcher.getInstance().dispatchEvent(new GoToSlideEvent(slideID,prevSlideID,nextSlideID));
						if((model.prevAS!=prevAnimStep)||(model.nextAS!=nextAnimStep)){
							CairngormEventDispatcher.getInstance().dispatchEvent(new GoToAnimationStepEvent(animStep,prevAnimStep,nextAnimStep));
						}
						
					}
					
				}
			}
			catch(err:Error)
			{	
				//CairngormEventDispatcher.getInstance().dispatchEvent(new CairngormErrorEvent(err,"Error in VideoPlayerChange command occured!"));
				trace("VideoPlayerChange. Error occured! Error message: "+err.message);
			}				
			
		}		
		
	}
}